package com.onenine.app.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.onenine.app.R;
import com.onenine.app.fragment.InviteTabFragment;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.tab.CommonTabLayout;
import com.onenine.base.tab.listener.CustomTabEntity;
import com.onenine.base.tab.listener.OnTabSelectListener;
import com.onenine.base.tab.utils.TextTabEntity;

import java.util.ArrayList;

/**
 * 创建邀请问答
 */
public class InviteReplyCreateActivity extends BaseActivity implements View.OnClickListener
        , OnTabSelectListener, ViewPager.OnPageChangeListener {

    private CommonTabLayout mTabLayout;
    private ViewPager mViewPager;
    private View mBtnBack;

    public static void launchActivity(Context context, String postId, String postContent) {
        Intent intent = new Intent(context, InviteReplyCreateActivity.class);
        intent.putExtra("postId", postId);
        intent.putExtra("postContent", postContent);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_invite_tab_list;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mTabLayout = findViewById(R.id.tl_tab);
        mTabLayout.setTextSelectColor(RoleResLoader
                .getRoleColorResourceId(RoleResLoader.DynamicResName.THEME_COLOR));
        mViewPager = findViewById(R.id.vp_pager);
        mBtnBack = findViewById(R.id.btn_back);
    }

    @Override
    protected void initListeners() {
        mBtnBack.setOnClickListener(this);
        mTabLayout.setOnTabSelectListener(this);
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    protected void initData() {
        ArrayList<CustomTabEntity> tabEntityList = new ArrayList<>();
        String[] titles = getResources().getStringArray(R.array.invite_tab_title);
        for (String title : titles) {
            tabEntityList.add(new TextTabEntity(title));
        }
        mViewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), titles.length));
        mTabLayout.setTabData(tabEntityList);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_back) {
            finish();
        }
    }

    @Override
    public void onTabSelect(int position) {
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTabLayout.setCurrentTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private class MyFragmentAdapter extends FragmentStatePagerAdapter {

        private int mTabCount;

        public MyFragmentAdapter(FragmentManager fm, int tabCount) {
            super(fm);
            mTabCount = tabCount;
        }

        @Override
        public int getCount() {
            return mTabCount;
        }

        @Override
        public Fragment getItem(int position) {
            return InviteTabFragment.newInstance(getIntent().getExtras(), position);
        }
    }

}
